package com.xyhy.core.base.model.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

@Getter
@Setter
@Accessors(chain = true)
@ApiModel("core:base:树节点VO")
public class TreeNodeVO<ID> {
  @ApiModelProperty("父节点")
  private ID id;

  @ApiModelProperty("节点标签")
  private String label;

  @ApiModelProperty("父节点")
  private ID parentId;

  @ApiModelProperty("是否禁用该节点")
  private Boolean disabled;

  @ApiModelProperty("扩展属性，子节点列表")
  private List<? extends TreeNodeVO<ID>> children;

  @ApiModelProperty("界面属性，是否叶节点")
  private Boolean isLeaf;

  @ApiModelProperty("界面属性，节点图标")
  private String icon;

  @ApiModelProperty("界面属性，是否展开")
  private boolean expand;

//  public <T extends TreeNodeVO<ID>> void addChild(T node) {
//    if (children == null) {
//      children = new ArrayList<>();
//    }
//    children.add(node);
//  }

  public boolean getIsLeaf() {
    return Optional.ofNullable(isLeaf)
            .orElseGet(() -> Optional.ofNullable(children).map(x -> CollectionUtils.isEmpty(x)).orElse(false));
  }

}
